/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Bill.java
 *
 * Created on Oct 1, 2012, 9:43:09 AM
 */
package Bill;

import ConnnectData.DataConnect;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import model.modelTable;

/**
 *
 * @author HP
 */
public class Bill extends javax.swing.JInternalFrame {

    /** Creates new form Bill */
    Connection con;
    Statement sta;
    ResultSet res;
    public Bill() {
        initComponents();
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Bill.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Bill.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Bill.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Bill.class.getName()).log(Level.SEVERE, null, ex);
        }
        try{
                con = DataConnect.connectDB();
               sta = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 
            }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbBill = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnDeleteBill = new javax.swing.JMenu();
        mnShowBill = new javax.swing.JMenu();
        mnClose = new javax.swing.JMenu();

        setTitle("Bill Management");
        setPreferredSize(new java.awt.Dimension(1056, 481));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Bill_1.png"))); // NOI18N

        tbBill.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbBill);

        mnDeleteBill.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/delete.png"))); // NOI18N
        mnDeleteBill.setText("Delete Bill");
        mnDeleteBill.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnDeleteBillMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnDeleteBill);

        mnShowBill.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/star.gif"))); // NOI18N
        mnShowBill.setText("Show Bill");
        mnShowBill.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnShowBillMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnShowBill);

        mnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/exit.png"))); // NOI18N
        mnClose.setText("Close");
        mnClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnCloseMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnClose);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(268, 268, 268)
                .addComponent(jLabel1)
                .addContainerGap(310, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1052, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(65, 65, 65)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void mnShowBillMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnShowBillMouseClicked
// TODO add your handling code here:
    try {
        String sql = "select Bill.Bill_Id,Customer_Name, Product_Name, Payment_Methods, convert(char(12), Create_Date,101)as Create_Date , Bill_Detail.Product_Quantity,"
                + " Product.Product_Price, Total_amount, Staff_Name from ((((Bill_Detail inner join Product on Product.Product_Id=Bill_Detail.Product_Id)"
                + "inner join Bill on Bill_Detail.Bill_Id=Bill.Bill_Id)"
                + "inner join Customer on Customer.Customer_Id=Bill.Customer_Id) inner join Staff on Staff.Staff_Id=Bill.Staff_Id) where Bill_status = 1 order by Create_Date asc";
        res = sta.executeQuery(sql);
        tbBill.setModel(new modelTable(res));
    } catch (Exception e) {
    }
}//GEN-LAST:event_mnShowBillMouseClicked

private void mnDeleteBillMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnDeleteBillMouseClicked
// TODO add your handling code here:
     if (tbBill.getSelectedRow() != -1) {
            int remove = JOptionPane.showConfirmDialog(this, "Are you sure want delete this Bill !");
            if (remove == 0) {
                int index = Integer.parseInt(tbBill.getValueAt(tbBill.getSelectedRow(), 0).toString());
                try {
                    String sql_del = "Update Bill set Bill_status = 0 where Bill_Id=" + index;
                    sta.executeUpdate(sql_del);
                    res = sta.executeQuery("select Bill.Bill_Id,Customer_Name, Product_Name, Payment_Methods, convert(char(12), Create_Date,101)as Create_Date , Bill_Detail.Product_Quantity,"
                + " Product.Product_Price, Total_amount, Staff_Name from ((((Bill_Detail inner join Product on Product.Product_Id=Bill_Detail.Product_Id)"
                + "inner join Bill on Bill_Detail.Bill_Id=Bill.Bill_Id)"
                + "inner join Customer on Customer.Customer_Id=Bill.Customer_Id) inner join Staff on Staff.Staff_Id=Bill.Staff_Id) where Bill_status = 1  order by Create_Date asc");
                    tbBill.setModel(new modelTable(res));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                JOptionPane.showMessageDialog(this, "Delete Bill success ! ");
            }

        } else {
            JOptionPane.showMessageDialog(null, "You should choose the Bill want to delete !");
        }
}//GEN-LAST:event_mnDeleteBillMouseClicked

private void mnCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnCloseMouseClicked
// TODO add your handling code here:
     int Close = JOptionPane.showConfirmDialog(null, "Are you sure want close  !");
    if (Close == 0) {
        this.dispose();
    }
}//GEN-LAST:event_mnCloseMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenu mnClose;
    private javax.swing.JMenu mnDeleteBill;
    private javax.swing.JMenu mnShowBill;
    private javax.swing.JTable tbBill;
    // End of variables declaration//GEN-END:variables
}
